﻿using AutoMapper;
using B.S.RbacData.Domain;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.RBAC;
using B.S.RbacData.Read.API.Dtos;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.RBAC
{
    /// <summary>
    /// 获取角色信息命令处理
    /// </summary>

    public class GetAllRoleCommandhandlter : IRequestHandler<GetAllRoleCommand, ApiResult<List<GetAllRoleDtos>>>
    {
        private readonly IBaseRepository<RoleModel> roleRep;
        private readonly IMapper mapper;

        public GetAllRoleCommandhandlter(IBaseRepository<RoleModel> roleRep,IMapper mapper)
        {
            this.roleRep = roleRep;
            this.mapper = mapper;
        }



        public async Task<ApiResult<List<GetAllRoleDtos>>> Handle(GetAllRoleCommand request, CancellationToken cancellationToken)
        {


            //获取角色信息
            var rolelist = roleRep.GetAll().AsNoTracking().Where(x=>!x.IsDeleted).ToList();




            return new ApiResult<List<GetAllRoleDtos>>()
            {

                code = ResultEnumCode.Ok,
                message = "查询成功",
                data = mapper.Map<List<GetAllRoleDtos>>(rolelist)

            };
        }
    }
}
